#!/usr/sbin/rsct/perl5/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2002 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 

use strict;
use Getopt::Std;
use POSIX qw(strftime);

$::CTMSG="/usr/sbin/rsct/bin/ctdspmsg";
$::MSGMAPPATH="/usr/sbin/rsct/msgmaps";


my ($strCmd,@unix_name);
my ($community, $host,$rc,$agent);

my @unix_name = `/bin/uname -a 2>&1`;
if($? >> 8){
    return 128;
}

$::os = (split(' ', $unix_name[0]))[0]; 


sub usage {
    my $msg;
    if($::os =~ m/AIX/i){
    	chomp($msg = `$::CTMSG snmpevent bridges.snmp.cat IMsgSNMPEVENT_USAGE_AIX`);
    }elsif($::os =~ m/Linux/i){
    	chomp($msg = `$::CTMSG snmpevent bridges.snmp.cat IMsgSNMPEVENT_USAGE_LINUX`);
    }
    print $msg;
    exit 1; 
}

# Parse the cmd line args and check them
sub getArgs {

    if (! getopts('ha:c:H:') ) { &usage; }
    if ($::opt_h) { &usage; }
    if ($::opt_H && $::opt_a) { &usage; }
    # get the name of the host we will be sending the trap to
    if($::opt_H){
	if($::os =~ m/Linux/i){   # make sure this is AIX.
	    $host = $::opt_H;   # if it's AIX assign the host name!
	}else{                
	    print "Error: AIX can not directly send traps to SNMP managers!\n";
	    exit 1;
	}
    # get the name of the agent we will be sending the trap to
    }elsif($::opt_a){
	if($::os =~ m/AIX/i){   # make sure this is AIX.
	    $agent = $::opt_a;   # if it's AIX assign the host name!
	}else{                
	    print "Error: Linux does not have Agent it connects to!\n";
	    exit 1;
	}
    # if no host is specified, we send it to the localhost
    }else{
	$host = defined($ENV{SNMP_HOST}) ? $ENV{SNMP_HOST} : "localhost";
	$agent = defined($ENV{SNMP_HOST}) ? $ENV{SNMP_HOST} : "localhost";
    }
    
    # get the community name we will be sending the trap to
    if($::opt_c){
	$community = $::opt_c;
    # if no community is specified, we default to public:
    }else{
	$community = defined($ENV{SNMP_COMMUNITY}) ? $ENV{SNMP_COMMUNITY}
                                                   : "public";
    }
}



my $where = join(' ', @ARGV);
my $convertedTime = strftime("%A %D %T", localtime($ENV{ERRM_TIME}) );
my $str = "$ENV{ERRM_COND_SEVERITY} $ENV{ERRM_TYPE} occurred. Condition=$ENV{ERRM_COND_NAME} Node=$ENV{ERRM_NODE_NAME} Resource=$ENV{ERRM_RSRC_NAME} Resource Class=$ENV{ERRM_RSRC_CLASS_NAME} Resource Attribute=$ENV{ERRM_ATTR_NAME} Attribute Type=$ENV{ERRM_DATA_TYPE} Attribute Val=$ENV{ERRM_VALUE}";


&getArgs();

# if AIX then we call the aix snmptrap command:

if($::os =~ m/AIX/i){
   $strCmd = "snmptrap -a $agent -c $community -m $str";
}

# else if Linux, then we call the NET-SNMP snmptrap command:

elsif($::os =~ m/Linux/i){
   $strCmd = "/usr/bin/snmptrap -v 2c -c $community $host '' ";
   $strCmd .= "IBM-RSCT-MIB::errmEvent ";
   $strCmd .= "errmCondSeverity s \"$ENV{ERRM_COND_SEVERITY}\" ";
   $strCmd .= "errmType s \"$ENV{ERRM_TYPE}\" ";
   $strCmd .= "errmCondName s \"$ENV{ERRM_COND_NAME}\" ";
   $strCmd .= "errmNodeName s \"$ENV{ERRM_NODE_NAME}\" ";
   $strCmd .= "errmRsrcName s \"$ENV{ERRM_RSRC_NAME}\" ";
   $strCmd .= "errmRsrcClassName s \"$ENV{ERRM_RSRC_CLASS_NAME}\" ";
   $strCmd .= "errmAttrName s \"$ENV{ERRM_ATTR_NAME}\" ";
   $strCmd .= "errmDataType s \"$ENV{ERRM_DATA_TYPE}\" ";
   $strCmd .= "errmValue s \"$ENV{ERRM_VALUE}\" ";

}

# execute the snmptrap command:

$rc = system("$strCmd"); 
$rc = $rc >> 8;
exit $rc;
